package com.xinzhi.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.Goods;
import com.xinzhi.domain.User;
import com.xinzhi.domain.entity.Orders;
import com.xinzhi.domain.entity.need.OrderRequest;
import com.xinzhi.service.LoginService;
import com.xinzhi.service.OrderService;
import com.xinzhi.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private LoginService loginService;

    /**
     * 点击去支付生成新订单
     */
    @RequestMapping(value = "/generate",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String newOrder(@RequestBody List<OrderRequest> list,HttpServletRequest request){
        return JSON.toJSONString(orderService.newOrder(list,request));
    }

    /**
     * 个人页面 查看所有订单/各种状态的订单
     * @param request
     * @param state
     * @param nowPage
     * @return
     */
    @RequestMapping(value = "/watch",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String seeOrder(HttpServletRequest request, @Param("state") Integer state, @Param("nowPage") Integer nowPage){


        return JSON.toJSONString(orderService.seeOrdersByState(loginService.checkToken(request.getHeader("token")).getUserId(),state,nowPage));
    }

    /**
     * 后台 查看所有订单/各种状态的订单
     * @param applicantId  自提点id
     * @param state        状态
     * @param nowPage
     * @return
     */
    @RequestMapping(value = "/underWatch",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String underOrder(@Param("applicantId") Integer applicantId, @Param("state") Integer state, @Param("nowPage") Integer nowPage){
        return JSON.toJSONString(orderService.underOrdersInfo(applicantId,state,nowPage));
    }

    /**
     * 付款成功，取货成功
     * @param orderNum
     * @param state
     * @return
     */
    @RequestMapping(value = "/updateOrder",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String changeOrderState(@Param("OrderNum") String orderNum, @Param("state") Integer state){
        System.out.println("走到这了");
        return JSON.toJSONString(orderService.updateOrderState(orderNum,state));
    }


    /**
     * 展示所有自提人
     * @param request
     * @return
     */
    @RequestMapping(value = "/showMan",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String showMan(HttpServletRequest request){
        return JSON.toJSONString(orderService.showManById(loginService.checkToken(request.getHeader("token")).getUserId()));
    }

    /**
     * 展示所有自提点
     * @return
     */
    @RequestMapping(value = "/showPoint",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String showPoint(){
        return JSON.toJSONString(orderService.showPoint());
    }

    /**
     * 跳转到确认订单页面携带数据
     * @return
     */
    @RequestMapping(value = "/goShop",produces = "text/plain;charset=utf-8")
    @ResponseBody
    @CrossOrigin
    public String goShop(Integer productId,HttpServletRequest request){
        // wzk 修改 参数列表  原代码：HttpServletRequest request，Integer productId
//        System.out.println(productId);
//        if(productId != null && productId ==""){
//            id = Integer.valueOf(productId);
//        }
        //============================================
        System.out.println("到了");
        return JSON.toJSONString(orderService.goBuy(loginService.checkToken(request.getHeader("token")).getUserId(),productId));  // id 原代码：productId
    }


    @PostMapping("/buy")
    public String buy(@RequestParam String orderNum) {
        return JSON.toJSONString(orderService.pay(orderNum));
    }




}
